package cn.gok.manage.service.impl;

import cn.gok.dao.CategoryMapper;
import cn.gok.manage.service.CategoryService;
import cn.gok.pojo.Category;
import cn.gok.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    CategoryMapper categoryMapper;


    @Override
    public ResultData getCategoryById(Integer parentId) {
        List<Category> category = categoryMapper.queryChildByParentId(parentId);
        System.out.println(category);

        if (category != null){
            return ResultData.createData(category);
        }
        return ResultData.fail("未找到该品类");
    }




    @Override
    public ResultData insertCategoryParentIdAndName(Integer parentId,String name) {
        int i = categoryMapper.insertCategoryParentIdAndName(parentId,name);
        System.out.println(i);

        if (i > 0){
            return ResultData.success("添加品类成功");
        }
        return ResultData.fail("添加品类失败");
    }

    @Override
    public ResultData updateNameById(Integer id, String name) {

        int i = categoryMapper.updateNameById(id, name);
        System.out.println(i);
        if (i > 0) {
            return ResultData.success("更新品类成功");

        }
        return ResultData.fail("更新品类失败");
    }

    @Override
    public ResultData getDeepCategory(int categoryId) {
        List<Integer> nums = new ArrayList<>();
        nums = deepPrintCategoryId(categoryId,nums);
        return ResultData.createData(nums);
    }

    private List<Integer> deepPrintCategoryId(int categoryId,List<Integer> nums){
        int location = 0;
        List<Category> categories = categoryMapper.queryCategoryByCategoryId(categoryId);
        if (categories.size() == 0)
            return nums;
        for (Category category : categories) {
            nums.add(category.getId());
            nums = deepPrintCategoryId(category.getId(),nums);
        }
        return nums;
    }
}
